package com.zeedev.flyingsaucer.adapter;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckedTextView;
import android.widget.ResourceCursorAdapter;
import com.zeedev.flyingsaucer.data.BeerUri;

public class ColumnCursorAdapter extends ResourceCursorAdapter {

//	BeerFilterProvider mFilterProvder;
//	BeerFilter mFilter = null;
	String groupBy;
	BeerUri beerUri;
	
	public ColumnCursorAdapter(Context context, int layout, Cursor c, BeerUri beerFilterIn, String groupBy) {
		super(context, layout, c);
		//this.setAutoCheck(autoCheckIn);
		//this.property = propertyIn;
//		this.mFilterProvder = beerFilterIn;
		this.groupBy = groupBy;
//		mFilter = beerFilterIn.getBeerFilter();
		this.beerUri = beerFilterIn;
	}
	
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		CheckedTextView textView = (CheckedTextView) view.findViewById(android.R.id.text1);
		textView.setText(cursor.getString(cursor.getColumnIndex(groupBy)));
		textView.setChecked(beerUri.getGroupedSelectionSet(groupBy).contains(cursor.getString(cursor.getColumnIndex(groupBy))));
		textView.setOnClickListener(checkClickedListener);
	}

	private OnClickListener checkClickedListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			CheckedTextView tv = (CheckedTextView) v;
			Boolean checked = tv.isChecked();
			if (checked) {
				beerUri.getGroupedSelectionSet(groupBy).remove(tv.getText().toString());
			} else {
				beerUri.getGroupedSelectionSet(groupBy).add(tv.getText().toString());
			}
			tv.setChecked(!checked);
			
		}
		
	};
	

}
